import { request } from '../../request/index.js'
import { getLocation } from '../../utils/asyncWx.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: [],
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 2000,
    duration: 500,
    QueryParams: {
      longitude: "",
      latitude: ""
    },
    //控制二维码显隐 默认隐藏
    showApp: true,
    //轮播图切换索引
    index: 0,
    //社区数组
    communityList: [],
    //团长数组
    groupList: [],
    //选好的社区和团长  传递给头部组件
    communityObj: {
      // communityName: '航开路',
      // groupName: '富力城'
    },
    //维保商品分类
    categoryArr: [],
  },
  handleLocation() {
    let { show } = this.data
    this.setData({
      show: !show
    })
  },
  //点击选择团长
  chooseGroup(e) {
    const { index } = e.currentTarget.dataset
    let { groupList, show, communityObj } = this.data
    groupList.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    let i = groupList.findIndex(v => v.isActive === true)
    communityObj.groupName = groupList[i].groupLeaderName
    wx.setStorageSync("communityObj", communityObj);
    this.setData({
      groupList,
      show: !show,
      communityObj
    })
  },
  //点击选择社区
  async chooseCommunity(e) {
    const { index } = e.currentTarget.dataset
    let { communityList, communityObj } = this.data
    communityList.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    let i = communityList.findIndex(v => v.isActive === true)
    communityObj.communityName = communityList[i].communityName
    const community = communityList[i].communityId
    const res = await request({ url: '/community/groupLeader/selectList', data: { community } })
    console.log(res)
    this.setData({
      communityList,
      communityObj,
      groupList: res.data
    })
  },
  //控制二维码显隐
  handleShow() {
    let { showApp } = this.data
    showApp = !showApp
    this.setData({
      showApp
    })
  },
  //轮播图切换事件
  indexChange(e) {
    this.setData({
      index: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMaintenanceClass()
  },
  //获取维保商品分类数据
  async getMaintenanceClass() {
    const res = await request({ url: '/maintenance/goods/classify/home' })
    this.setData({
      categoryArr: res.data
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //获取轮播图数组
  async getBannerList() {
    const bannerType = 2
    const token = wx.getStorageSync("token");
    const header = { token }
    const res = await request({ url: "/banner/" + bannerType, data: { bannerType }, header })
    this.setData({
      background: res.data
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    const res = await getLocation()
    let { QueryParams } = this.data
    QueryParams.longitude = res.longitude
    QueryParams.latitude = res.latitude
    this.setData({
      QueryParams
    })
    const communityObj = wx.getStorageSync("communityObj");
    this.setData({
      communityObj
    })
    this.getCommunity()
    this.getGroupList()
    this.getBannerList()
  },
  //获取社区数组
  async getCommunity() {
    const { QueryParams, communityObj } = this.data
    const res = await request({ url: '/community/selectList', data: QueryParams })
    this.setData({
      communityList: res.data
    })
  },
  //获取团长数组
  async getGroupList() {
    const { communityObj } = this.data
    const community = communityObj.communityId
    const res = await request({ url: '/community/groupLeader/selectList', data: { community } })
    console.log(res)
    this.setData({
      groupList: res.data
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})